package com.lin.missyou.repository;

import com.lin.missyou.model.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.Optional;

public interface OrderRepository extends JpaRepository<Order,Long> {

    // status ==  unpaid 延迟消息队列
    // expiredTime > now
    // uid
    Page<Order> findByUserIdAndStatusAndExpiredTimeGreaterThan(Long uid, Integer status, Date now, Pageable pageable);


    Page<Order> findByUserIdAndStatus(Long uid,Integer status,Pageable pageable);
    // 查询全部
    Page<Order> findByUserId(Long uid,Pageable pageable);

    Optional<Order> findFirstByUserIdAndId(Long uid,Long oid);

    Optional<Order> findFirstByOrderNo(String orderNo);

    @Modifying
    @Query("update Order o set o.status = :status where o.orderNo = :orderNo")
    int updateOrderStatus(String orderNo,Integer status);

    @Modifying
    @Query("update Order o set o.status = 5 where o.id = :oid and o.status = 1")
    int cancelOrder(long oid);
}
